%Function to read the messay Jicamarca magnetometer data
%The result is put into a struct similar to that returned by INTREMAG.m
% May 27, 2011

function[data, err] = read_jicamarca_mag_min(fname)

err = 0;

[header, data] = hdrload(fname);

if size(data,2) < 8,
    err = 1;
    data = [];
else
    %minutely, first array is matlab time (have follow the INTERMAG.m)      

    MINUTELY(:,1) = datenum(data(:,3),data(:,2),data(:,1),data(:,4),data(:,5),30)';
    %
    MINUTELY(:,[2 3]) = HDtoXY(deg2rad(data(:,6)), data(:,7));
    
    MINUTELY(:,4) = data(:,8);
    data = struct('MINUTELY',MINUTELY);
end;





function XY = HDtoXY(D,H)
XY = [H.*cos(D) H.*sin(D)];